import axios from 'axios';
import { getRedirectPath } from '../../common/getRedirectPath.js';

const initState = {
    name:'',
    password:'',
    repeatpassword:'',
    type:'',
    msg:'',
    isAuth:false,
    redirectPath:''
}

export const ACTIONS = {
    REGIST_SUCCESS : 'REGIST_SUCCESS',
    LOGIN_SUCCESS : 'LOGIN_SUCCESS',
    MSG : "MSG",
    PERFECT : 'PERFECT',
    RESETPATH:'RESETPATH',
    OUTLOGIN:'OUTLOGIN',
    LOADUSERINFO:'LOADUSERINFO'
}

// reducer 导出，参数于store中(多则合并)  reducer最终返回newstate
export function userReducer(state=initState,action){
    switch(action.type){
        case ACTIONS.REGIST_SUCCESS:
            return {...state,msg:'注册成功',isAuth:true, ...action.payload,redirectPath:getRedirectPath(action.payload)};
        case ACTIONS.LOGIN_SUCCESS:
            return {...state,msg:'登陆成功',isAuth:true, ...action.payload,redirectPath:getRedirectPath(action.payload)};
        case ACTIONS.PERFECT:
            return {...state,msg:'更新成功',isAuth:true, ...action.payload,redirectPath:getRedirectPath(action.payload)};
        case ACTIONS.MSG:
            return {...state, isAuth:false, msg:action.payload};
        case ACTIONS.RESETPATH:
            return {...state, isAuth:false, redirectPath:'',msg:''};
        case ACTIONS.OUTLOGIN:
            return {...state,msg:'退出登录成功'};
        case ACTIONS.LOADUSERINFO:
            return {...state}
        default:
            return state
    }
}

// 【action】
export function loadUseinfoData(){
    return (dispatch)=>{
        axios.get('/userapi/info').then(res => {
            if(res.status === 200){
                if(res.data.code === 0){
                    dispatch(registSuccess(res.data.data));
                }else{
                    dispatch(Msg(res.data.msg));
                }
            }
        })
    }
}

export function registSuccess(data){
    return {type:ACTIONS.REGIST_SUCCESS,payload:data}
}

export function Msg(msg){
    return {type:ACTIONS.MSG,payload:msg}
}

export function loginSuccess(data){
    return {type:ACTIONS.LOGIN_SUCCESS,payload:data}
}

export function perfectSuccess(data){
    return {type:ACTIONS.PERFECT,payload:data}
}
/**
 * 重置redirectPath
 */
export function resetPath(path){
    return {type:ACTIONS.RESETPATH,path}
}

// 注册
export function regist(data){
    if(!data.name || !data.password){
        return Msg('用户名或密码不能为空');
    }
    if(!data.repeatpassword){
        return Msg('请填写确认密码');
    }
    if(data.password !== data.repeatpassword){
        return Msg('确认密码与密码不一致');
    }
    // 异步
    return (dispatch) => {
        axios.post('/userapi/regist',data).then(res => {
            if(res.status === 200){
                if(res.data.code === 0){
                    // dispatch 【action】 && 传值data
                    dispatch(registSuccess(data));
                }else{
                    // 此处msg是后端msg
                    dispatch(Msg(res.data.msg));
                }
            }
        })
    }
}


// 登陆
export function login(data){
    if(!data.name || !data.password){
        return Msg('用户名或密码不能为空');
    }
    // 异步
    return (dispatch) => {
        axios.post('/userapi/login',data).then(res => {
            if(res.status === 200){
                if(res.data.code === 0){
                    // dispatch action && 传值data
                    dispatch(loginSuccess(res.data.data));
                }else{
                    // 此处msg是后端msg
                    dispatch(Msg(res.data.msg));
                }
            }
        })
    }
}

// 完善Boss信息
export function improveBossSubmit(data){
    if(!data.avatar){
        return Msg('请选择头像');
    }
    if(!data.position){
        return Msg('请选择招聘职位');
    }
    if(!data.company){
        return Msg('请填写公司名称');
    }
    if(!data.paymoney){
        return Msg('请填写薪资待遇');
    }
    if(!data.ask){
        return Msg('请填写招聘要求');
    }
    // 异步
    return (dispatch) => {
        axios.post('/userapi/perfect',data).then(res => {
            if(res.status === 200){
                if(res.data.code === 0){
                    // res.data.data有type & name
                    // 传到store中的参数都是请求响应过来的结果
                    dispatch(perfectSuccess(res.data.data));
                }else{
                    dispatch(Msg(res.data.msg));
                }
            }
        })
    }
}


/**
 * 完善求职者信息
 */
export function improveJobSubmit(data){
    if(!data.avatar){
        return Msg('请选择头像');
    }
    if(!data.position){
        return Msg('请选择招聘职位');
    }
    if(!data.skills){
        return Msg('请填写专业技能');
    }
    if(!data.desc){
        return Msg('请填写自我描述');
    }
    // 异步
    return (dispatch) => {
        axios.post('/userapi/perfect',data).then(res => {
            if(res.status === 200){
                if(res.data.code === 0){
                    console.log(res)
                    dispatch(perfectSuccess(res.data.data));
                }else{
                    dispatch(Msg(res.data.msg));
                }
            }
        })
    }
}

/**
 * 退出登录
 */
export function outLogin(){
    return {type:ACTIONS.OUTLOGIN};
}